//
//  ViewContactViewController.swift
//  HandOffTest
//
//  Created by Lyy on 15/8/19.
//  Copyright (c) 2015年 Lyy. All rights reserved.
//

import UIKit

class ViewContactViewController: UITableViewController,UITableViewDataSource,UITableViewDelegate {

    var contact: Contact!
    let activityTypeViewContact = "com.yy.handofftest.view-contact"
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        if let userInfo = userActivity?.userInfo {
            contact = Contact()
            contact.getContactDataFromDictionary(userInfo)
            tableView.reloadData()
        }
        createUserActivity()
    }
    
    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        self.performSegueWithIdentifier("idUnwindSegueViewContact", sender: self)
    }
    
    func createUserActivity() {
        userActivity = NSUserActivity(activityType: activityTypeViewContact)
        userActivity!.title = "View Contact"
        userActivity?.becomeCurrent()
    }
    
    override func restoreUserActivityState(activity: NSUserActivity) {
        userActivity = activity
        super.restoreUserActivityState(activity)
    }
    
    override func updateUserActivityState(activity: NSUserActivity) {
        if activity == activityTypeViewContact {
            userActivity?.userInfo = contact.getDictionaryFromContactData() as [NSObject : AnyObject]
        }
        super.updateUserActivityState(activity)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let validContact = contact {
            return 4
        } else {
            return 0
        }
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("viewContactIdentifier", forIndexPath: indexPath) as! UITableViewCell

        switch indexPath.row{
        case 0:
            cell.textLabel?.text = contact.firstname
            cell.detailTextLabel?.text = "First name"
            
        case 1:
            cell.textLabel?.text = contact.lastname
            cell.detailTextLabel?.text = "Last name"
            
        case 2:
            cell.textLabel?.text = contact.phoneNumber
            cell.detailTextLabel?.text = "Phone number"
            
        default:
            cell.textLabel?.text = contact.email
            cell.detailTextLabel?.text = "E-mail"
        }
        
        return cell
    }
    
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 60.0
    }
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "idUnwindSegueViewContact" {
            self.userActivity?.invalidate()
        }
    }

    /*
    // Override to support conditional editing of the table view.
    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return NO if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {
            // Delete the row from the data source
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        } else if editingStyle == .Insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return NO if you do not want the item to be re-orderable.
        return true
    }
    */

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */

}
